package com.zhongzhi.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

/**
 * 远程连接工具类
 * @author Administrator
 *
 */

public class ClientUtil {

	private static CloseableHttpClient httpclient = HttpClients.createDefault();


	/**get请求获取结果*/
	public static String Gethttp(String s) {
		URI uri=null;
		HttpGet httpGet=null;
		CloseableHttpResponse response = null;

		try {
			uri = new URI(s);
		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}
		httpGet = new HttpGet(uri);
		try {
			response = httpclient.execute(httpGet);
			if (response.getStatusLine().getStatusCode()==200) {
				return EntityUtils.toString(response.getEntity());
			}else {
				return null;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	//post请求获取结果
	public static String Posthttp(String s,Object o) {

		return null;
	}

	/**解析json字符串(对象)*/
	public static <T> T Jsonobj(String s,Class<T> T ){
		T t = JSON.parseObject(s, T);
		return t;
	}
	/**解析json字符串(数组)*/
	public static <T> List<T> Jsonlist(String s,Class<T> T ) {
		List<T> t = JSON.parseArray(s, T);
		return t;
	}
}
